// pages/admin/admin.js
Page({
  data: {
    title: '',
    content: '',
  },

  onLoad() {
    // 获取 openid 并检查本地存储的用户信息
    console.log('onLoad');
  },

  inputTitle(e) {
    this.setData({
      title: e.detail.value,
    });
  },

  inputContent(e) {
    this.setData({
      content: e.detail.value,
    });
  },

  publishAnnouncement() {
    if (!this.data.title || !this.data.content) {
      wx.showToast({
        title: '请输入完整的公告内容',
        icon: 'none',
      });
      return;
    }

    const db = wx.cloud.database();
    db.collection('announcements').add({
      data: {
        title: this.data.title,
        content: this.data.content,
        timestamp: new Date(),
      }
    }).then(() => {
      wx.showToast({
        title: '公告发布成功',
        icon: 'success',
      });
    }).catch(err => {
      wx.showToast({
        title: '公告发布失败',
        icon: 'none',
      });
      console.error('公告发布失败', err);
    });
  },
});

  
